﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;

using AKMII.DMRA.Common;
using AKMII.DMRA.Business.Management;
using AKMII.DMRA.Business.Rule;

namespace AKMII.DMRA.Web.Share
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        #region proteced methods
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataList();
            }
        }

        protected string GetMonthName(string monthIndex)
        {
            switch (int.Parse(monthIndex))
            {
                case 1:
                    return "January";
                case 2:
                    return "February";
                case 3:
                    return "March";
                case 4:
                    return "April";
                case 5:
                    return "May";
                case 6:
                    return "June";
                case 7:
                    return "July";
                case 8:
                    return "August";
                case 9:
                    return "September";
                case 10:
                    return "October";
                case 11:
                    return "November";
                case 12:
                    return "December";
            }
            return "";
        }
        #endregion

        #region private methods
        private void BindDataList()
        {
            OrderManager om = new OrderManager();
            Order order = om.GetOrder(User.Identity.Name, CurrentPeriod.Month, CurrentPeriod.Year);
            if (order == null)
            {
                order = new Order();
                order.PeroidMonth = CurrentPeriod.Month;
                order.PeroidYear = CurrentPeriod.Year;
                order.AccountId = User.Identity.Name;
            } 
            Session["CartZips"] = order.Zips;
            List<LeadBlock> zips = new List<LeadBlock>();
            zips = (List<LeadBlock>)Session["CartZips"];

            int total = 0;
            int sum = 0;
            int activeleads = 0;
            int lapseleads = 0;
            int bussinessleads = 0;
            int neighborleads = 0;

            if (Session["CartZips"] != null)
            {
                for (int i = 0; i < zips.Count; i++)
                {
                    switch (CurrentPeriod.Month)
                    {
                        case 1:
                            if (zips[i].IsOrderInJan)
                            {
                                sum = sum + zips[i].ActiveLeadCount + zips[i].LapsedLeadCount;
                                activeleads = activeleads + zips[i].ActiveLeadCount;
                                lapseleads = lapseleads + zips[i].LapsedLeadCount;
                                bussinessleads = bussinessleads + zips[i].BusinessLeadCount;
                                neighborleads = neighborleads + zips[i].NeighborLeadCount;
                            }
                            break;
                        case 2:
                            if (zips[i].IsOrderInFeb)
                            {
                                sum = sum + zips[i].ActiveLeadCount + zips[i].LapsedLeadCount;
                                activeleads = activeleads + zips[i].ActiveLeadCount;
                                lapseleads = lapseleads + zips[i].LapsedLeadCount;
                                bussinessleads = bussinessleads + zips[i].BusinessLeadCount;
                                neighborleads = neighborleads + zips[i].NeighborLeadCount;
                            }
                            break;
                        case 3:
                            if (zips[i].IsOrderInMar)
                            {
                                sum = sum + zips[i].ActiveLeadCount + zips[i].LapsedLeadCount;
                                activeleads = activeleads + zips[i].ActiveLeadCount;
                                lapseleads = lapseleads + zips[i].LapsedLeadCount;
                                bussinessleads = bussinessleads + zips[i].BusinessLeadCount;
                                neighborleads = neighborleads + zips[i].NeighborLeadCount;
                            }
                            break;
                        case 4:
                            if (zips[i].IsOrderInApr)
                            {
                                sum = sum + zips[i].ActiveLeadCount + zips[i].LapsedLeadCount;
                                activeleads = activeleads + zips[i].ActiveLeadCount;
                                lapseleads = lapseleads + zips[i].LapsedLeadCount;
                                bussinessleads = bussinessleads + zips[i].BusinessLeadCount;
                                neighborleads = neighborleads + zips[i].NeighborLeadCount;
                            }
                            break;
                        case 5:
                            if (zips[i].IsOrderInMay)
                            {
                                sum = sum + zips[i].ActiveLeadCount + zips[i].LapsedLeadCount;
                                activeleads = activeleads + zips[i].ActiveLeadCount;
                                lapseleads = lapseleads + zips[i].LapsedLeadCount;
                                bussinessleads = bussinessleads + zips[i].BusinessLeadCount;
                                neighborleads = neighborleads + zips[i].NeighborLeadCount;
                            }
                            break;
                        case 6:
                            if (zips[i].IsOrderInJun)
                            {
                                sum = sum + zips[i].ActiveLeadCount + zips[i].LapsedLeadCount;
                                activeleads = activeleads + zips[i].ActiveLeadCount;
                                lapseleads = lapseleads + zips[i].LapsedLeadCount;
                                bussinessleads = bussinessleads + zips[i].BusinessLeadCount;
                                neighborleads = neighborleads + zips[i].NeighborLeadCount;
                            }
                            break;
                        case 7:
                            if (zips[i].IsOrderInJul)
                            {
                                sum = sum + zips[i].ActiveLeadCount + zips[i].LapsedLeadCount;
                                activeleads = activeleads + zips[i].ActiveLeadCount;
                                lapseleads = lapseleads + zips[i].LapsedLeadCount;
                                bussinessleads = bussinessleads + zips[i].BusinessLeadCount;
                                neighborleads = neighborleads + zips[i].NeighborLeadCount;
                            }
                            break;
                        case 8:
                            if (zips[i].IsOrderInAug)
                            {
                                sum = sum + zips[i].ActiveLeadCount + zips[i].LapsedLeadCount;
                                activeleads = activeleads + zips[i].ActiveLeadCount;
                                lapseleads = lapseleads + zips[i].LapsedLeadCount;
                                bussinessleads = bussinessleads + zips[i].BusinessLeadCount;
                                neighborleads = neighborleads + zips[i].NeighborLeadCount;
                            }
                            break;
                        case 9:
                            if (zips[i].IsOrderInSep)
                            {
                                sum = sum + zips[i].ActiveLeadCount + zips[i].LapsedLeadCount;
                                activeleads = activeleads + zips[i].ActiveLeadCount;
                                lapseleads = lapseleads + zips[i].LapsedLeadCount;
                                bussinessleads = bussinessleads + zips[i].BusinessLeadCount;
                                neighborleads = neighborleads + zips[i].NeighborLeadCount;
                            }
                            break;
                        case 10:
                            if (zips[i].IsOrderInOct)
                            {
                                sum = sum + zips[i].ActiveLeadCount + zips[i].LapsedLeadCount;
                                activeleads = activeleads + zips[i].ActiveLeadCount;
                                lapseleads = lapseleads + zips[i].LapsedLeadCount;
                                bussinessleads = bussinessleads + zips[i].BusinessLeadCount;
                                neighborleads = neighborleads + zips[i].NeighborLeadCount;
                            }
                            break;
                        case 11:
                            if (zips[i].IsOrderInNov)
                            {
                                sum = sum + zips[i].ActiveLeadCount + zips[i].LapsedLeadCount;
                                activeleads = activeleads + zips[i].ActiveLeadCount;
                                lapseleads = lapseleads + zips[i].LapsedLeadCount;
                                bussinessleads = bussinessleads + zips[i].BusinessLeadCount;
                                neighborleads = neighborleads + zips[i].NeighborLeadCount;
                            }
                            break;
                        case 12:
                            if (zips[i].IsOrderInDec)
                            {
                                sum = sum + zips[i].ActiveLeadCount + zips[i].LapsedLeadCount;
                                activeleads = activeleads + zips[i].ActiveLeadCount;
                                lapseleads = lapseleads + zips[i].LapsedLeadCount;
                                bussinessleads = bussinessleads + zips[i].BusinessLeadCount;
                                neighborleads = neighborleads + zips[i].NeighborLeadCount;
                            }
                            break;
                    }
                    total = total + zips[i].ActiveLeadCount + zips[i].LapsedLeadCount;
                }
            }

            this.lbActiveLead.Text = activeleads.ToString();
            this.lbBussinessLead.Text = bussinessleads.ToString();
            this.lbLapseLead.Text = lapseleads.ToString();
            this.lbNeighborLead.Text = neighborleads.ToString();
            this.lbAllLeads.Value = sum.ToString();
            this.lbCartAgent.Text = sum.ToString();
            this.lblRemain.Text = (Convert.ToInt32(GetActiveLeadCount()) - sum).ToString();

            GdvMonthSchedule.DataSource = SystemConfiguration.MonthSettings;
            GdvMonthSchedule.DataBind();
        }
        #endregion

        #region public methods
        public string GetActiveLeadCount()
        {
            string leadCount = string.Empty;

            User user = Session["CurrentUser"] as User;

            if (user != null)
            {
                leadCount = (user.AgentNumber * SystemConfiguration.CommonSetting.LeadPerAgent * 4).ToString();
            }
            else
            {
                FormsAuthentication.SignOut();
                Response.Redirect(Constants.LoginUrl);
            }

            return leadCount;
        }
        #endregion


    }
}
